package com.xs.bookspoststation.StudentActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xs.bookspoststation.BookActivity.BookViewActivity;
import com.xs.bookspoststation.BookActivity.UpdateBookActivity;
import com.xs.bookspoststation.R;
import com.xs.bookspoststation.bean.Book;
import com.xs.bookspoststation.bean.Student;
import com.xs.bookspoststation.db.DBHelperStudent;

public class UpdateStudentActivity extends AppCompatActivity implements View.OnClickListener{
    //定义对象
    private EditText edit_update_sno,edit_update_sname,edit_update_spwd,
            edit_update_sphone,edit_update_sclass,edit_update_spro,edit_update_sde;
    private RadioGroup stu_update_radioGroup2;
    private RadioButton stu_update_radioButton1,stu_update_radioButton2;
    private Button btn_update_stu_cancel,btn_update_stu;
    //定义一个数据库帮助类
    private DBHelperStudent dbHelperStudent;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_student);
        //操作对象的方法
        initView();
    }

    private void initView() {
        //获取学号控件
        edit_update_sno = findViewById(R.id.edit_update_sno);
        //获取姓名控件
        edit_update_sname = findViewById(R.id.edit_update_sname);
        //获取密码控件
        edit_update_spwd = findViewById(R.id.edit_update_spwd);
        //获取电话控件
        edit_update_sphone = findViewById(R.id.edit_update_sphone);
        //获取班级控件
        edit_update_sclass = findViewById(R.id.edit_update_sclass);
        //获取专业控件
        edit_update_spro = findViewById(R.id.edit_update_spro);
        //获取系部控件
        edit_update_sde = findViewById(R.id.edit_update_sde);
        //获取性别
        stu_update_radioGroup2 = findViewById(R.id.stu_update_radioGroup2);
        //获取性别女控件
        stu_update_radioButton1 = findViewById(R.id.stu_update_radioButton1);
        //获取性别男控件
        stu_update_radioButton2 = findViewById(R.id.stu_update_radioButton2);
        //获取返回控件
        btn_update_stu_cancel = findViewById(R.id.btn_update_stu_cancel);
        //获取添加控件
        btn_update_stu = findViewById(R.id.btn_update_stu);
        //点击事件
        btn_update_stu_cancel.setOnClickListener(this);
        btn_update_stu.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperStudent = new DBHelperStudent(UpdateStudentActivity.this,"student.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperStudent.getWritableDatabase();
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_update_stu_cancel:
                Intent intent=new Intent(UpdateStudentActivity.this, StudentViewActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一界面", Toast.LENGTH_LONG).show();
                startActivity(intent);
                break;
            case R.id.btn_update_stu:
                goUpdate();
                break;
        }
    }

    /**
     * 界面启动执行此方法
     */
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=getIntent();
        //以下是获取查询图书信息界面传过来的数据
        // 获取用户输入的数据
        String stu_sno = intent.getStringExtra("sno");
        String stu_name = intent.getStringExtra("sname");
        String stu_pwd = intent.getStringExtra("spwd");
        String stu_phone = intent.getStringExtra("sphone");
        String stu_class = intent.getStringExtra("sclass");
        String stu_pro = intent.getStringExtra("spro");
        String stu_de = intent.getStringExtra("sde");
        //将这些获取的数据进行显示
        edit_update_sno.setText(stu_sno);
        //设置图书编号不能修改
        edit_update_sno.setEnabled(false);
        edit_update_sname.setText(stu_name);
        edit_update_spwd.setText(stu_pwd);
        edit_update_sphone.setText(stu_phone);
        edit_update_sclass.setText(stu_class);
        edit_update_spro.setText(stu_pro);
        edit_update_sde.setText(stu_de);
        //打印日志
        Log.v("操作","！");
    }
    /**
     * 修改方法
     */
    private void goUpdate() {
        //如果点击了更新
        //还是判断一下是否填写完整，
        if(edit_update_sno.length()==0||edit_update_sname.length()==0||edit_update_spwd.length()==0||
                edit_update_sphone.length()==0||edit_update_sclass.length()==0||
                edit_update_spro.length()==0||edit_update_sde.length()==0){
            Toast.makeText(UpdateStudentActivity.this, "信息未填写完整！", Toast.LENGTH_LONG).show();
        }else{
            String wantUpdateSno = edit_update_sno.getText().toString();
            String wantUpdateSname = edit_update_sname.getText().toString();
            String wantUpdateSpwd = edit_update_spwd.getText().toString();
            String wantUpdateSphone = edit_update_sphone.getText().toString();
            String wantUpdateSclass = edit_update_sclass.getText().toString();
            String wantUpdateSpro = edit_update_spro.getText().toString();
            String wantUpdateSde = edit_update_sde.getText().toString();
            Student student = new Student(wantUpdateSno,wantUpdateSname,wantUpdateSpwd,null,wantUpdateSphone,wantUpdateSclass,wantUpdateSpro,wantUpdateSde);
            //数据库操作
            SQLiteDatabase db = dbHelperStudent.getWritableDatabase();//
            if(db.isOpen()){
                String sno = student.getSno();
                String stu_name = student.getSname();
                String stu_pwd = student.getSpwd();
                String stu_phone = student.getSphone();
                String stu_class = student.getSclass();
                String stu_pro = student.getSpro();
                String stu_de = student.getSde();
                //类似于HashMap，但这个只能存储基本类型
                ContentValues c = new ContentValues();
                c.put("sno", sno);
                c.put("sname", stu_name);
                c.put("spwd", stu_pwd);
                c.put("sphone", stu_phone);
                c.put("sclass", stu_class);
                c.put("spro", stu_pro);
                c.put("sde", stu_de);
                db.update("studentInfo", c, "sno=?",new String[]{sno});
                Log.v("更新信息操作", "更新信息成功！");
            }else{
                Toast.makeText(UpdateStudentActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
            }
            Toast.makeText(UpdateStudentActivity.this,"学生信息修改成功！",Toast.LENGTH_LONG).show();
            Intent intent2 =new Intent(UpdateStudentActivity.this, StudentViewActivity.class);
            startActivity(intent2);
        }

    }
}
